package me.ctidy.assignment04.activity.assets;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import me.ctidy.assignment04.R;
import me.ctidy.assignment04.adapter.AssetsAdapter;
import me.ctidy.assignment04.constant.AssetsType;
import me.ctidy.assignment04.entity.Assets;

import java.util.ArrayList;
import java.util.List;

/**
 * AssetsListActivity
 *
 * @author ctidy
 */
public class AssetsListActivity extends AppCompatActivity {

    ListView lvAssets;
    List<Assets> data;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assets_list);
        initUI();
    }

    private void initUI() {
        // I. 绑定控件
        lvAssets = findViewById(R.id.aal_lv_assets);

        // II. 获取数据
        data = sampleData();

        // III. 绑定适配器
        lvAssets.setAdapter(new AssetsAdapter(this, data));

        // IV. 注册子元素点击监听器，并绑定到 ListView 的每一项元素上
        lvAssets.setOnItemClickListener((parent, view, position, id) -> {
            startActivity(
                    new Intent(this, AssetsDetailActivity.class)
                            .putExtra("assets", data.get(position))
            );
        });
    }

    private List<Assets> sampleData() {
        ArrayList<Assets> data = new ArrayList<>();
        data.add(new Assets(1L, "老公工资", AssetsType.CARD, 120000.0, "建行"));
        data.add(new Assets(6L, "老婆工资", AssetsType.CARD, 90000.0, "工行"));
        data.add(new Assets(2L, "理财基金", AssetsType.ALIPAY, 240000.0, "余额宝"));
        data.add(new Assets(3L, "活动资金", AssetsType.WECHAT, 3000.0, ""));
        data.add(new Assets(7L, "儿子", AssetsType.ALIPAY, 500.0, "余额宝"));
        data.add(new Assets(4L, "现金", AssetsType.CASH, 500.0, "紧急备用"));
        data.add(new Assets(5L, "医保", AssetsType.OTHER, 2000.0, ""));
        return data;
    }

}
